package com.lagou.filter;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.rpc.*;

import java.util.Arrays;
import java.util.logging.Logger;


@Activate(group = {CommonConstants.CONSUMER,CommonConstants.PROVIDER_SIDE})
public class DubboInvokeFilter   implements Filter {
   /* @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
      *//*  long   startTime  = System.currentTimeMillis();
        try {
            // 执行方法
            return  invoker.invoke(invocation);
        } finally {
            System.out.println("invoke time:"+(System.currentTimeMillis()-startTime) + "毫秒");
        }*//*
         RpcContext.getContext()
        .setInvoker(invoker)
        .setInvocation(invocation)
        .setLocalAddress(NetUtils.getLocalHost(),0)
                 .setRemoteAddress(invoker.getUrl().getHost(), invoker.getUrl().getPort());
         if(invocation instanceof RpcInvocation){
                     ((RpcInvocation) invocation).setInvoker(invoker);
        }
         try  {
                  return invoker.invoke(invocation);
         }finally {
                   RpcContext.getContext().clearAttachments();
         }

    }*/
   private static Logger log = Logger.getLogger("MonitorService");
    private static Logger msgLogger = Logger.getLogger("Message");
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation)
            throws RpcException {
        // TODO Auto-generated method stub
        Result result = null;

        msgLogger.info("IP:" + RpcContext.getContext().getRemoteHost()
                + ", Service:" + invoker.getInterface().getName()
                + ", Method:" + invocation.getMethodName()
                + ", Request:{" + Arrays.toString(invocation.getArguments())
                + "}."
        );

        Long startTime = System.currentTimeMillis();
        try {
            result = invoker.invoke(invocation);
            if (result.getException() instanceof Exception) {
                throw new Exception(result.getException());
            }
        } catch (Exception e) {

            System.out.println("IP:" + RpcContext.getContext().getRemoteHost()
                    + ", Service:" + invoker.getInterface().getName()
                    + ", Method:" + invocation.getMethodName()
                    + ", Exception:{" + e.toString() + "}.");
        } finally {
            msgLogger.info("IP:" + RpcContext.getContext().getRemoteHost()
                    + ", Service:" + invoker.getInterface().getName()
                    + ", Method:" + invocation.getMethodName()
                    + ", Response:{" + result.toString()
                    + "}."
            );

            log.info("IP:" + RpcContext.getContext().getRemoteHost()
                    + ", Service:" + invoker.getInterface().getName()
                    + ", Method:" + invocation.getMethodName()
                    + ", consume: " + (System.currentTimeMillis()-startTime) + " ms."
            );
        }

        return result;
    }


}
